function varargout = gui_preferences(varargin)
% GUI_PREFERENCES MATLAB code for gui_preferences.fig
%      GUI_PREFERENCES, by itself, creates a new GUI_PREFERENCES or raises the existing
%      singleton*.
%
%      H = GUI_PREFERENCES returns the handle to a new GUI_PREFERENCES or the handle to
%      the existing singleton*.
%
%      GUI_PREFERENCES('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUI_PREFERENCES.M with the given input arguments.
%
%      GUI_PREFERENCES('Property','Value',...) creates a new GUI_PREFERENCES or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before gui_preferences_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to gui_preferences_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help gui_preferences

% Last Modified by GUIDE v2.5 20-Jul-2012 13:13:09

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @gui_preferences_OpeningFcn, ...
                   'gui_OutputFcn',  @gui_preferences_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before gui_preferences is made visible.
function gui_preferences_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to gui_preferences (see VARARGIN)

% Choose default command line output for gui_preferences
try
    handles.output = 0;

    % Update handles structure
    guidata(hObject, handles);

    gui_main = find(strcmp(varargin, 'gui_main'));
    if (isempty(gui_main)) || ...
            (length(varargin) <= gui_main) || ...
            (~ishandle(varargin{gui_main+1}))
        dontOpen = true;
    else
        handles.gui_main = varargin{gui_main+1};
        handlesMain = guidata(handles.gui_main);
        handles.pref = handlesMain.pref;
        guidata(hObject, handles);

        set(handles.default_scratch_text, 'String',...
            handles.pref.scratch_directory);
        set(handles.default_scratch2_text, 'String',...
            handles.pref.scratch_directory2);

        num_film = size(handles.pref.film_type,2);
        for i = 1:num_film
            menustr{i} = sprintf('%s (%+d)', handles.pref.film_type(i).name, ...
                handles.pref.film_type(i).offset);
        end
        menustr{num_film+1} = 'New (Specify) ...';

        set(handles.default_film_type_popup, 'String', menustr);
        set(handles.default_film_type_popup, 'Value', ...
            handles.pref.film_type_default);

        % UIWAIT makes gui_project_settings wait for user response (see UIRESUME)
        uiwait(hObject);
    end
catch exception
    gui_exception(exception);
    dontOpen = true;
end


% --- Outputs from this function are returned to the command line.
function varargout = gui_preferences_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;
delete(hObject);


function default_scratch_text_Callback(hObject, eventdata, handles)
% hObject    handle to default_scratch_text (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of default_scratch_text as text
%        str2double(get(hObject,'String')) returns contents of default_scratch_text as a double


% --- Executes during object creation, after setting all properties.
function default_scratch_text_CreateFcn(hObject, eventdata, handles)
% hObject    handle to default_scratch_text (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function default_scratch2_text_Callback(hObject, eventdata, handles)
% hObject    handle to default_scratch2_text (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of default_scratch2_text as text
%        str2double(get(hObject,'String')) returns contents of default_scratch2_text as a double


% --- Executes during object creation, after setting all properties.
function default_scratch2_text_CreateFcn(hObject, eventdata, handles)
% hObject    handle to default_scratch2_text (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in default_film_type_popup.
function default_film_type_popup_Callback(hObject, eventdata, handles)
% hObject    handle to default_film_type_popup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns default_film_type_popup contents as cell array
%        contents{get(hObject,'Value')} returns selected item from default_film_type_popup
try
    idx = get(hObject,'Value');
    string_list = get(hObject,'String');
    if(idx == size(string_list,1))
        answer = inputdlg({'Film type:', 'Sound separtion value:'},...
            'Custom Film Type', [1 30;1 30], {'Custom', '+0'});
        if length(answer)
            string_list{idx+1} = string_list{idx};
            offset = str2num(answer{2});
            string_list{idx} = sprintf('%s (%+d)', answer{1}, offset);
            set(hObject,'String',string_list);

            handles.pref.film_type(idx).name = answer{1};
            handles.pref.film_type(idx).offset = offset;

        else
            % reset it to the previous value
            set(hObject,'Value',handles.pref.film_type_default);
        end
    end
    handles.pref.film_type_default = idx;
    guidata(hObject, handles);
catch exception
    gui_exception(exception);
end

% --- Executes during object creation, after setting all properties.
function default_film_type_popup_CreateFcn(hObject, eventdata, handles)
% hObject    handle to default_film_type_popup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in default_scratch_browse_button.
function default_scratch_browse_button_Callback(hObject, eventdata, handles)
% hObject    handle to default_scratch_browse_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
try
    dirname = uigetdir(get(hObject,'String'),'Scratch Directory');
    if isequal(dirname, 0)
        return
    else
        % Add a file separator (i.e., \ for Windows or / for Linux) at the end
        % of the directory name in case it does not have one.
        if (strcmpi(dirname(end), filesep) == false)
            dirname = [dirname filesep];
        end
        set(handles.default_scratch_text,'String',dirname);
        handles.pref.scratch_directory = dirname;
        guidata(hObject, handles);
    end 
catch exception
    gui_exception(exception);
end


% --- Executes on button press in default_scratch2_browse_button.
function default_scratch2_browse_button_Callback(hObject, eventdata, handles)
% hObject    handle to default_scratch2_browse_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
try
    dirname = uigetdir(get(hObject,'String'),'Scratch Directory');
    if isequal(dirname, 0)
        return
    else
        % Add a file separator (i.e., \ for Windows or / for Linux) at the end
        % of the directory name in case it does not have one.
        if (strcmpi(dirname(end), filesep) == false)
            dirname = [dirname filesep];
        end
        set(handles.default_scratch2_text,'String',dirname);
        handles.pref.scratch_directory2 = dirname;
        guidata(hObject, handles);
    end 
catch exception
    gui_exception(exception);
end


% --- Executes on button press in ok_button.
function ok_button_Callback(hObject, eventdata, handles)
% hObject    handle to ok_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
try
    mainHandles = guidata(handles.gui_main);
    handles.pref.scratch_directory = get(handles.default_scratch_text, 'String');
    handles.pref.scratch_directory2 = get(handles.default_scratch2_text, 'String');

    if(isempty(handles.pref.scratch_directory) || ...
    		isempty(handles.pref.scratch_directory2) )
    	warndlg('Both scratch directory fields are mandatory');
    	return;
	end

    mainHandles.pref = handles.pref;
    guidata(handles.gui_main, mainHandles);

	% write the new settings to the userfile
	pref = handles.pref;
	save(mainHandles.userfile, 'pref');

    handles.output = 1;
    guidata(hObject, handles);
    gui_close_figure(handles.preferences_figure);
catch exception
    gui_exception(exception);
end

% --- Executes during object creation, after setting all properties.
function preferences_figure_CreateFcn(hObject, eventdata, handles)
% hObject    handle to preferences_figure (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes when user attempts to close preferences_figure.
function preferences_figure_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to preferences_figure (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
try
    gui_close_figure(handles.preferences_figure);
catch exception
    gui_exception(exception);
end


% --- Executes on button press in cancel_button.
function cancel_button_Callback(hObject, eventdata, handles)
% hObject    handle to cancel_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
try
    gui_close_figure(handles.preferences_figure);
catch exception
    gui_exception(exception);
end
